//
//  UseCarYuYue.m
//  xjairOA
//
//  Created by apple on 16-1-27.
//  Copyright (c) 2016年 net. All rights reserved.
//

#import "UseCarYuYue.h"
#import "Constants.h"
#import "UserCarSelectPerson.h"

@implementation UseCarYuYue

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title=@"申请用车";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    NSDictionary * params = @{@"model":@"loadonedept"};
    self.appDelegate = [UIApplication sharedApplication].delegate;
    [self.appDelegate.manager
     GET:UserCarMutiAction
     parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         self.data = responseObject;
         //[tableData addObjectsFromArray:responseObject];
         
         //         NSDictionary* dict = [notices objectAtIndex:0];
         //         self.biaoti.text = [dict objectForKey:@"title"];
         //         self.neirong.text = [dict objectForKey:@"desc"];
         NSDictionary * dict = [self.data objectAtIndex:0];
         UILabel* label = (UILabel *)[self.view viewWithTag:1];
         label.text = [dict objectForKey:@"deptName"];
         self.deptId = [dict objectForKey:@"id"];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         UIAlertView * alert = [[UIAlertView alloc]
                                initWithTitle:@"提示"
                                message:@"获取服务器响应出错！"
                                delegate:nil
                                cancelButtonTitle:@"确定"
                                otherButtonTitles:nil];
         [alert show];
     }];
    
    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonSystemItemCancel target:self action:@selector(commit)];
    [self.navigationItem setRightBarButtonItem:rightButton];
}
//-(void)finish
//{
//    UIAlertView * alert = [[UIAlertView alloc]
//                           initWithTitle:@"提示"
//                           message:@"有用了！"
//                           delegate:nil
//                           cancelButtonTitle:@"确定"
//                           otherButtonTitles:nil];
//    [alert show];
//}

-(void)commit
{
    NSString * dept = ((UILabel *)[self.view viewWithTag:1]).text;
    NSString * useCarPeoples = ((UILabel *)[self.view viewWithTag:2]).text;
    NSString * useCarNumOfPeople = ((UILabel *)[self.view viewWithTag:3]).text;
    NSString * startDate = ((UILabel *)[self.view viewWithTag:4]).text;
    NSString * endDate = ((UILabel *)[self.view viewWithTag:5]).text;
    NSString * useForward = ((UITextField *)[self.view viewWithTag:7]).text;
    NSString * useScope;
    switch ([((UISegmentedControl*)[self.view viewWithTag:6]) selectedSegmentIndex]) {
        case 0:
            useScope = @"市区";
            break;
        case 1:
            useScope = @"长途";
            break;
        case 2:
            useScope = @"机场院内";
            break;
        case 3:
            useScope = @"其他";
            break;
        default:
            break;
    }
    NSString * useUsed = ((UITextField *)[self.view viewWithTag:8]).text;
    NSString * diao = ((UILabel*)[self.view viewWithTag:9]).text;
    NSString * shen;
    if (((UILabel*)[self.view viewWithTag:10]).enabled) {
        shen = ((UILabel*)[self.view viewWithTag:10]).text;
    }else{
        shen = @"";
    }
    NSDictionary * params = @{@"model":@"addBookCar",@"useDept":dept,@"endDate":endDate,@"startDate":startDate
                              ,@"useForward":useForward,@"useNum":useCarNumOfPeople,@"usePeople":useCarPeoples
                              ,@"useScope":useScope,@"useUsed":useUsed,@"diao":diao,@"shen":shen};
    [self.appDelegate.manager
     POST:UserCarMutiAction
     parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         self.data = responseObject;
         //[tableData addObjectsFromArray:responseObject];
         
         //         NSDictionary* dict = [notices objectAtIndex:0];
         //         self.biaoti.text = [dict objectForKey:@"title"];
         //         self.neirong.text = [dict objectForKey:@"desc"];
         NSDictionary * dict = [self.data objectAtIndex:0];
         if ([((NSString*)[dict objectForKey:@"r"]) isEqualToString:@"success"] ) {
             UIAlertView * alert = [[UIAlertView alloc]
                                    initWithTitle:@"提示"
                                    message:@"提交成功！"
                                    delegate:nil
                                    cancelButtonTitle:@"确定"
                                    otherButtonTitles:nil];
             [alert show];
         }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         UIAlertView * alert = [[UIAlertView alloc]
                                initWithTitle:@"提示"
                                message:@"获取服务器响应出错！"
                                delegate:nil
                                cancelButtonTitle:@"确定"
                                otherButtonTitles:nil];
         [alert show];
     }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    if (indexPath.row==0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell1" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
       
    }else if(indexPath.row==1){
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell2" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
       
    }else if(indexPath.row==2){
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell3" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
    }else if(indexPath.row==3){
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell4" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
    }else if(indexPath.row==4){
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell5" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
    }else if(indexPath.row==5){
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell6" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
    }else if(indexPath.row==6){
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell7" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
    }else if(indexPath.row==7){
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell8" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
    }else if(indexPath.row==8){
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell9" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
    }else if(indexPath.row==9){
        cell = [tableView dequeueReusableCellWithIdentifier:@"usecarYuYueCell10" forIndexPath:indexPath];
        cell.layer.cornerRadius=12;
        cell.layer.masksToBounds=YES;
    }
    return cell;
}

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if ([identifier isEqualToString:@"useCarShenSegue"]) {
        return NO;
    }else{
        return YES;
    }
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UITableViewCell* cell = (UITableViewCell*)sender;
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    //NSLog(@"indexpath.row is %ld",(long)indexPath.row);
    if (indexPath.row == 1) {
        UserCarSelectPerson* useCarSelectPerson = (UserCarSelectPerson*)segue.destinationViewController;
        useCarSelectPerson.deptId = self.deptId;
    }
//    if (indexPath.row==9) {
//        if ( ((UILabel*)[self.view viewWithTag:10]).enabled) {
 //           [self performSegueWithIdentifier:@"useCarShenSegue" sender:self];
//        }
        //NSLog(@"你点击了选择审批人");
//    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [((UITextField*)[self.view viewWithTag:7]) resignFirstResponder];
    [((UITextField*)[self.view viewWithTag:8]) resignFirstResponder];
    if (indexPath.row==9) {
        if ( ((UILabel*)[self.view viewWithTag:10]).enabled){
            [self performSegueWithIdentifier:@"useCarShenSegue" sender:self];
        }
    }
}

- (IBAction)segmentChanged:(id)sender {
    switch ([sender selectedSegmentIndex]) {
        case 1:
            ((UILabel*)[self.view viewWithTag:10]).enabled=YES;
            ((UILabel*)[self.view viewWithTag:10]).text=@"点击选择";
            break;
            
        default:
            ((UILabel*)[self.view viewWithTag:10]).enabled=NO;
            ((UILabel*)[self.view viewWithTag:10]).text=@"不能选择";
            break;
    }
}

- (IBAction)backTouchDown:(id)sender {
    [((UITextField*)[self.view viewWithTag:7]) resignFirstResponder];
    [((UITextField*)[self.view viewWithTag:8]) resignFirstResponder];
}
@end
